﻿$(document).ready(function() {
    var url = DiretorioBase + "Relatorios/LoadRelatorioOrdemServico";
    $('#geraRelatorio').click(function() {
        var parDataInicial = $('#DataInicial').val();
        var parDataFinal = $('#DataFinal').val();
        var parStatus =  $('#Status').val();
        var parCliente = $('#Cliente').val();

        $('#report').empty();
        ShowLoading('#report', 'buscando Ordens de Serviço...','medium');

        $.getJSON
        (
            url,
            {
                DataInicial: parDataInicial,
                DataFinal: parDataFinal,
                Status: parStatus,
                Cliente: parCliente 
            },
            loadReportOrdemServico
        );    
    })
})

function loadReportOrdemServico(result) {

    var mountReport = "";
    var totalRegistros = 0;
     
    mountReport += "<table>";
    mountReport += "<tr>";
    mountReport += "<th>Código</th>";
    mountReport += "<th>Data de Abertura</th>";
    mountReport += "<th>Cliente</th>";
    mountReport += "<th>Atendente</th>";
    mountReport += "<th>Solicitação</th>";
    mountReport += "<th>Local de Atendimento</th>";
    mountReport += "<th>Tipo de Atendimento</th>";
    mountReport += "<th>Status</th>";
    mountReport += "</tr>";

    $.each(result, function(i, item) {
        mountReport += "<tr>";
        mountReport += "<td>" + item.IdOrdemServico + "</td>";
        mountReport += "<td>" + fomataData(item.DataAbertura) + "</td>";
        mountReport += "<td>" + item.cliente.Nome + "</td>";
        mountReport += "<td>" + formataAtendente(item.Atendente) + "</td>";
        mountReport += "<td>" + item.ServicoSolicitado + "</td>";
        mountReport += "<td>" + FormatLocalAtendimento(item.LocalAtendimento) + "</td>";
        mountReport += "<td>" + FormatTipoAtendimento(item.TipoAtendimento) + "</td>";
        mountReport += "<td>" + FormatStatusOs(item.StatusOs) + "</td>";
        mountReport += "</tr>";

        totalRegistros++;
    });

    mountReport += "</table>";

    DestroyLoading();
    
    if (totalRegistros == 0) {
        $('#report').html('<span style="color:red;position:relative;top:5px;">Não existem registros para os parametros informados</span>');
    }
    else {
        mountReport += '<span style="color:red;position:relative;top:5px;">Total de Registros: ' + totalRegistros.toString() + '</span>';
        $('#report').html(mountReport);
    }
}


function fomataData(data)
{
    var m_names = ["01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"];
    var datetime = data;
    var dateString = datetime.replace(/[^+0-9]*/img, "");
    var dateObj = new Date(parseInt(dateString, 10));
    var curr_date = dateObj.getDate();
    var curr_month = dateObj.getMonth();
    var curr_year = dateObj.getFullYear();

    if (curr_date >= 1 && curr_date <= 9) {
        curr_date = '0' + curr_date.toString();
    }
    var returnValue = curr_date + "/" + m_names[curr_month] + "/" + curr_year;
    return returnValue;
}

function formataAtendente(atendente) {
    if (atendente == null) {
        return "Não Definido";
    }
    else {
        return atendente.UserName.toString();
    }
}


